﻿using AhpilyServer;
using System;
using System.Collections.Generic;

using System.Text;


namespace Server
{
    public interface IHandler
    {
        void RegisterHandler(HandlerCenter center);
    }

    public delegate void NetHandler(AhpilyServer.ClientPeer token,byte[] data);//1.修改绑定函数的委托参数

    public class HandlerCenter : IApplication
    {
        //存储所有的protocol和对应函数
        Dictionary<Protocol, NetHandler> handlers = new Dictionary<Protocol, NetHandler>();

        LoginHandler account = new LoginHandler();
        BattleHandler battle = new BattleHandler();

        public HandlerCenter()
        {
            account.RegisterHandler(this);
            battle.RegisterHandler(this);
        }

        public void AddHandler(Protocol cmd,NetHandler func)
        {
            handlers.Add(cmd,func);
        }

        public void OnDisconnect(AhpilyServer.ClientPeer client)
        {
            AccountCache.Instance.DelActor(client);
        }

        public void OnReceive(AhpilyServer.ClientPeer client, ByteBuffer buffer)
        {
            Protocol cmd = (Protocol)buffer.ReadInt();
            byte[] bytes = buffer.ReadBytes();

            handlers[cmd](client, bytes);
            buffer.Close();
        }
    }
}
